package cn.ffcs.bpm.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionSupport;

import cn.ffcs.base.web.PageControl;
import cn.ffcs.bpm.service.TransactorService;
import cn.ffcs.businessrequest.model.BusinessRequest;
import cn.ffcs.product.model.ProductReference;
import cn.ffcs.product.service.IProductReferenceService;
import cn.ffcs.security.model.User;
import cn.ffcs.share.model.ShareReference;
import cn.ffcs.share.service.IShareReferenceService;
import cn.ffcs.util.context.ApplicationContextUtil;

/**
 * 业务action层<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author liuzhh
 * @version $Id$
 */
public class OaRefAction extends ActionSupport
{
    /** 取数总数 **/
    private static int NUM_=5;
    /** 取数共享资料总数 **/
    private static int NUM_SHARE_=10;
    /** 产品资料服务 **/
    private IProductReferenceService productReferenceService;
    /** 共享资料服务 **/
    private IShareReferenceService shareReferenceService;
    /** 办理人服务 **/
    private TransactorService transactorService;
    /** 产品资料列表数据 **/
    private List<ProductReference> productRefs;
    /** 共享资料数据 **/
    private List<ShareReference> shareRefs;    
    /** 业务流程单数据集 **/
    private List<BusinessRequest> businessRequests;
    /** 当点用户 **/
    private User ssoUser;
    /** 用户标识**/
    private static final String LOGIN_USER_="LOGINUSER";
    
    
    public String oaRefData(){

        PrintWriter out = null; 
        HttpServletResponse response = ServletActionContext.getResponse();
        HttpServletRequest request = ServletActionContext.getRequest(); //取response对象       
//        response.setContentType("text/html");
        response.setContentType("text/html; charset=GBK");
        response.setHeader("Cache-Control","no-cache");           
        response.setCharacterEncoding("GBK");
        
        String outStr="var hdzx = [";
        String sUserID="";

        sUserID   = StringUtils.isBlank(request.getParameter("userID_s")) ? "" : request.getParameter("userID_s");
        String flag = "1";// StringUtils.isBlank(request.getParameter("flag")) ? "" : request.getParameter("userID");//1取总数，2取待办
        String misKey = StringUtils.isBlank(request.getParameter("misKey")) ? "" : request.getParameter("misKey");
        sUserID=sUserID.toUpperCase();

        if (flag.equalsIgnoreCase("1")){
          outStr+="'有 $ 件运营互动中心待办。',0,''";
            
            
//            User user=(User)request.getSession().getAttribute(LOGIN_USER_);
//            int num=0;
//            if(user!=null&&user.getId()>0){
//                Criterion cond1 = Restrictions.eq("userId", user.getPerson());
//                businessRequests = transactorService.findBy(cond1);
//                num=businessRequests.size();
//                outStr+="'有 $ 件运营互动中心待办。',"+num;
//            }else{
//                outStr+="'有 $ 件运营互动中心待办。',0";
//            }
//                outStr+=",'http://hdzx.fjtelecom.com/common/first.action?userID_s="+sUserID+"&misKey="+misKey+"'";
               
            
        }else{
//            Collection projects = business.getProjectList(sUserID, sysID,false);
//            
//            if (projects!=null && projects.size()>0){
//                Iterator it=projects.iterator();
//                int i=0;
//                while (it.hasNext()){
//                    homeListForm homeList =(homeListForm)it.next();
//                    
//                    if (i>0){
//                        outStr+=",";
//                    }
//                    outStr+="[";
//                    outStr+="'需求管理-"+homeList.getDbType()+"','";
//                    outStr+=homeList.getHandleType()+"','";
//                    outStr+=homeList.getCol4()+"','";
//                    outStr+=homeList.getUrgenceName()+"','";
//                    outStr+=homeList.getTopic()+"','http://";
//                    outStr+=request.getLocalAddr()+":"+request.getLocalPort()+request.getContextPath()+"/mis_index.jsp?OperatingUserID="+sUserID+"&pageurl="+homeList.getLink()+"'";
//                    outStr+="]";
//                    i++;
//                }
//            }
        }
//        
        
        outStr+="]";

        try
        {
            out = response.getWriter();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String tmp="";
        try
        {
            tmp=new String(outStr.getBytes(),"GBK");
        }
        catch (UnsupportedEncodingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out.print(tmp);
        return null;
    } 
    
    public String toForum(){
        
        User currUser=ApplicationContextUtil.getUserContextObject().getCurrUser();
        ssoUser=currUser;
        
        return SUCCESS;
    }

    /**
     * 获取产品资料服务
     * @return the productReferenceService
     */
    public IProductReferenceService getProductReferenceService()
    {
        return productReferenceService;
    }

    /**
     * 设置产品资料服务
     * @param productReferenceService the productReferenceService to set
     */
    public void setProductReferenceService(
            IProductReferenceService productReferenceService)
    {
        this.productReferenceService = productReferenceService;
    }

    /**
     * @return the productRefs
     */
    public List<ProductReference> getProductRefs()
    {
        return productRefs;
    }

    /**
     * @param productRefs the productRefs to set
     */
    public void setProductRefs(List<ProductReference> productRefs)
    {
        this.productRefs = productRefs;
    }

    /**
     * @return the ssoUser
     */
    public User getSsoUser()
    {
        return ssoUser;
    }

    /**
     * @param ssoUser the ssoUser to set
     */
    public void setSsoUser(User ssoUser)
    {
        this.ssoUser = ssoUser;
    }

    public IShareReferenceService getShareReferenceService()
    {
        return shareReferenceService;
    }

    public void setShareReferenceService(
            IShareReferenceService shareReferenceService)
    {
        this.shareReferenceService = shareReferenceService;
    }

    public List<ShareReference> getShareRefs()
    {
        return shareRefs;
    }

    public void setShareRefs(List<ShareReference> shareRefs)
    {
        this.shareRefs = shareRefs;
    }

    public TransactorService getTransactorService()
    {
        return transactorService;
    }

    public void setTransactorService(TransactorService transactorService)
    {
        this.transactorService = transactorService;
    }

    public List<BusinessRequest> getBusinessRequests()
    {
        return businessRequests;
    }

    public void setBusinessRequests(List<BusinessRequest> businessRequests)
    {
        this.businessRequests = businessRequests;
    }

}
